

if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
end

if ( CLIENT ) then

	SWEP.PrintName			= "Famas"			
	SWEP.Author				= "Counter-Strike"
	SWEP.Slot				= 2
	SWEP.SlotPos			= 0
	SWEP.IconLetter			= "t"
	SWEP.ViewModelFlip		= false
	killicon.AddFont( "weapon_famas", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )
	
end

SWEP.HoldType			= "ar2"
SWEP.Base				= "weapon_cs_base2_ds"

SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true

SWEP.ViewModel 			= "models/weapons/v_rif_famas.mdl"
SWEP.WorldModel 		= "models/weapons/w_rif_famas.mdl"

SWEP.Weight				= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false
SWEP.NeedToShoot		= 0
SWEP.LastShoot			= 0

SWEP.Primary.Sound			= Sound( "Weapon_FAMAS.Single" )
SWEP.Primary.Recoil			= 1.3
SWEP.Primary.Damage			= 11
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.013
SWEP.Primary.ClipSize		= 25
SWEP.Primary.Delay			= 0.054
SWEP.Primary.DefaultClip	= 115
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= "smg1"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Delay		= 0.55
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

function SWEP:PrimaryAttack()
	
	if !self:CanPrimaryAttack() or self.Owner:WaterLevel() > 2 or self.Weapon:Clip1() < 1 then return end
	// Play shoot sound

	// Shoot the bullet
	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
	local Crouch = ( self.Owner:Crouching() and self.Owner:OnGround() ) and 2 or 1
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Secondary.Delay )
	
	if self.Weapon:GetNetworkedBool( "Burst", false ) then
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Secondary.Delay )
		self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone )
		self.LastShoot = CurTime()
		self.NeedToShoot = 2
	else
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
		self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone / math.Clamp( ( CurTime() - LastShootTime ) * 1.3 , 0.001, 1 ) / Crouch )
	end
	
	if self.Owner:IsNPC() then return end

	// Punch the player's view
	self.Owner:ViewPunch( Angle( math.Rand( -0.5, -0.1 ) * self.Primary.Recoil, math.Rand( -0.5, 0.5 ) *self.Primary.Recoil, 0 ) )
	
	// In singleplayer this function doesn't get called on the client, so we use a networked float
	// to send the last shoot time. In multiplayer this is predicted clientside so we don't need to 
	// send the float.
	if ( (SinglePlayer() && SERVER) || CLIENT ) then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
	end
end

function SWEP:Think()
	if self.NeedToReload then
		self:Reload()
	end
	
	if self.Weapon:Clip1() < 1 and !self.Owner:KeyDown( IN_ATTACK ) and !self.AutoReloading then
		self:Reload()
		self.AutoReloading = true
		return
	end

	if self.Weapon:Clip1() > 0 then
		self.AutoReloading = false
	end

	if self.NeedToShoot > 0 and ( CurTime() - self.LastShoot ) > self.Primary.Delay / 3 then
		self.LastShoot = CurTime()
		self.NeedToShoot = self.NeedToShoot - 1
		if self.Weapon:Clip1() < 1 then return end
		self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone )
	end
end

function SWEP:SecondaryAttack()
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Secondary.Delay )
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Secondary.Delay )
	if self.Weapon:GetNetworkedBool( "Burst", false ) then
		self.Weapon:SetNetworkedBool( "Burst", false )
		self.Owner:PrintMessage( HUD_PRINTCENTER, "Switched to Automatic mode." )
	else
		self.Weapon:SetNetworkedBool( "Burst", true )
		self.Owner:PrintMessage( HUD_PRINTCENTER, "Switched to Burst mode." )
	end
end
